//
//  NBStatusListViewModel.swift
//  NBSinaWeibo
//
//  Created by ac on 16/5/25.
//  Copyright © 2016年 ac. All rights reserved.
//

import UIKit
import AFNetworking
//import SVProgressHUD

class NBStatusListViewModel: NSObject {
    
    var statuses: [NBStatusViewModel] = [NBStatusViewModel]()
    
    
    
    func loadData(isPulling: Bool, finished: (isSuccess: Bool) -> ()) -> (){
        
        var since_id: Int64 = 0
        var max_id: Int64 = 0
        
        if isPulling {
            max_id = statuses.last?.status?.id ?? 0
        } else {
            since_id = statuses.first?.status?.id ?? 0
        }

        
        ACNetworkTool.sharedTool.loadData(since_id,max_id: max_id) { (responseObject, error) in
            if error != nil{
                
                finished(isSuccess: false)
                return
            }
            let dict = (responseObject as? [String:AnyObject])!
            guard let array = dict["statuses"] as? [[String:AnyObject]] else{
                finished(isSuccess: false)
                return
                
            }
            var tempArr = [NBStatusViewModel]()
            for obj in array{
                let status = NBStatus(dict: obj)
//                tempArr.append(status)
//                self.statusArr.append(status)
                let statusVM = NBStatusViewModel(status: status)
                tempArr.append(statusVM)
            }
            //            let status = NBStatus(dict: dict)
//            self.statuses = tempArr
            if isPulling {
                //加载更多数据
                self.statuses = self.statuses + tempArr
            } else {
                //刷新新数据
                self.statuses = tempArr + self.statuses
            }

            

//            self.tableView.reloadData()
            finished(isSuccess: true)
        }
    }
}
